package com.woniuxy.oldobject.controller;

import com.woniuxy.oldobject.dto.ElderDto;
import com.woniuxy.oldobject.entity.ElderInfo;
import com.woniuxy.oldobject.entity.ResponseEntity;
import com.woniuxy.oldobject.service.ElderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 老人管理接口
 * @Title: ElderInfoController
 * @Package com.woniuxy.oldobject.controller
 * @Author: LSL
 * @CreateTime: 2022/7/25 15:32
 */
@RestController
@RequestMapping("/elderInfo")
public class ElderInfoController {

    @RequestMapping("/queryAllByifcare")
    public ResponseEntity queryAllByifcare(){
        List<ElderInfo> elderInfos = elderInfoService.queryAllByifcare();
        if (elderInfos!=null){
            return new ResponseEntity(200,"查询成功",elderInfos);
        }else {
            return ResponseEntity.ERROR;
        }
    }
    @Autowired
    ElderInfoService elderInfoService;

    /**
     * 通过id查询对应老人信息
     *
     * @param id：老人id
     * @return ResponseEntity：内部封装响应和老人信息
     */
    @GetMapping("/queryElderInfoById")
    public ResponseEntity queryElderInfoById(int id) {
        ElderInfo elderInfo = elderInfoService.queryById(id);
        if (elderInfo != null) {
            return new ResponseEntity(elderInfo);
        }
        return ResponseEntity.ERROR;
    }
    @RequestMapping("/unassignSeniors")
    public ResponseEntity unassignSeniors(@RequestBody ElderDto elderDto){
        boolean flag = elderInfoService.unassignSeniors(elderDto);
        if (flag){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.ERROR;
        }
    }

    /**
     * 通过护工id查询负责的所有老人
     *
     * @param staffId 护工id
     * @return
     */
    @GetMapping("/queryElderInfoBystaffId/{staffId}")
    public ResponseEntity queryElderInfoBystaffId( @PathVariable("staffId") int staffId) {
        List<ElderInfo> elderInfos = elderInfoService.queryElderInfoBystaffId(staffId);
        if (elderInfos != null) {
            return new ResponseEntity(elderInfos);
        }
        return ResponseEntity.ERROR;
    }
    @RequestMapping("/updateCondition")
    public ResponseEntity updateCondition(@RequestBody ElderDto elderDto){
        boolean flag = elderInfoService.updateCondition(elderDto);
        if (flag){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.ERROR;
        }
    }

    /**
     * 查询所有为分配护工的老人信息
     *
     * @return
     */
    @GetMapping("/queryElderInfoByCare")
    public ResponseEntity queryElderInfoByCare() {
        List<ElderInfo> elderInfos = elderInfoService.queryElderInfoByCare(0);
        if (elderInfos != null) {
            return new ResponseEntity(elderInfos);
        }
        return ResponseEntity.ERROR;
    }

    /**
     * 通过id删除老人信息
     *
     * @param id
     * @return
     */
    public ResponseEntity deleteElderById(int id) {
        int i = elderInfoService.deleteById(id);
        if (i == 1) {
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.ERROR;
    }

    /**
     * 通过id来修改老人的信息
     *
     * @param elderDto
     * @return
     */
    public ResponseEntity updateElder(ElderDto elderDto) {
        int update = elderInfoService.update(elderDto);
        if (update == 1) {
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.ERROR;
    }
}
